﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace S2_Xxxx_XxxNetApi
{
    /// <summary>
    /// 任务基类
    /// </summary>
    public abstract class BaseJob : IJob
    {
        private string _jobName = string.Empty;
        private string _gropName = string.Empty;
        private string _triggerName = string.Empty;
        private int _intervalTime;
        private DateTimeOffset _startTime;
        private DateTimeOffset? _endTime;
        private int _repeatCount;
        private string _cronExpression;


        /// <summary>
        /// 无参构造函数
        /// </summary>
        public BaseJob()
        {

        }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="jobName">任务名称</param>
        /// <param name="triggerName">触发器名称</param>
        /// <param name="gropName">任务组名称</param>
        /// <param name="intervalTime">间隔时间</param>
        public BaseJob(string jobName, string triggerName, string gropName, int intervalTime)
        {
            this._jobName = jobName;
            this._triggerName = triggerName;
            this._gropName = gropName;
            this._intervalTime = intervalTime;
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="jobName">任务名称</param>
        /// <param name="triggerName">触发器名称</param>
        /// <param name="gropName">任务组名称</param>
        /// <param name="intervalTime">间隔时间</param>
        /// <param name="cronExpression">cron表达式</param>
        public BaseJob(string jobName, string triggerName, string gropName, int intervalTime, string cronExpression)
        {
            this._jobName = jobName;
            this._triggerName = triggerName;
            this._gropName = gropName;
            this._intervalTime = intervalTime;
            this._cronExpression = cronExpression;
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="jobName">任务名称</param>
        /// <param name="triggerName">触发器名称</param>
        /// <param name="gropName">任务组名称</param>
        /// <param name="intervalTime">间隔时间</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        public BaseJob(string jobName, string triggerName, string gropName, int intervalTime, DateTimeOffset startTime, DateTimeOffset? endTime)
        {
            this._jobName = jobName;
            this._triggerName = triggerName;
            this._gropName = gropName;
            this._intervalTime = intervalTime;
            this._startTime = startTime;
            this._endTime = endTime;
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="jobName">任务名称</param>
        /// <param name="triggerName">触发器名称</param>
        /// <param name="gropName">任务组名称</param>
        /// <param name="intervalTime">间隔时间</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="repeatCount">重复次数</param>
        public BaseJob(string jobName, string triggerName, string gropName, int intervalTime, DateTimeOffset startTime, DateTimeOffset? endTime, int repeatCount)
        {
            this._jobName = jobName;
            this._triggerName = triggerName;
            this._gropName = gropName;
            this._intervalTime = intervalTime;
            this._startTime = startTime;
            this._endTime = endTime;
            this._repeatCount = repeatCount;
        }



        /// <summary>
        /// 任务名称
        /// </summary>
        public string JobName
        {
            get { return _jobName; }
            set { _jobName = value; }
        }

        /// <summary>
        /// 触发器名称
        /// </summary>
        public string TriggerName
        {
            get { return _triggerName; }
            set { _triggerName = value; }
        }

        /// <summary>
        /// 任务组名称
        /// </summary>
        public string GropName
        {
            get { return _gropName; }
            set { _gropName = value; }
        }

        /// <summary>
        /// 执行间隔时间
        /// </summary>
        public int IntervalTime
        {
            get { return _intervalTime; }
            set { _intervalTime = value; }
        }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTimeOffset StartTime
        {
            get { return _startTime; }
            set
            {
                if (value == null || value == DateTime.MinValue) _startTime = SystemTime.UtcNow();
                else _startTime = value;
            }
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTimeOffset? EndTime
        {
            get { return _endTime; }
            set { _endTime = value; }
        }

        /// <summary>
        /// 重复执行次数
        /// </summary>
        public int RepeatCount
        {
            get { return _repeatCount; }
            set { _repeatCount = value; }
        }

        /// <summary>
        /// Cron表达式
        /// </summary>
        public string CronExpression
        {
            get { return _cronExpression; }
            set { _cronExpression = value; }
        }


        /// <summary>
        /// 任务执行
        /// </summary>
        /// <param name="context">任务执行上下文</param>
        public abstract Task Execute(IJobExecutionContext context);
    }
}
